/**
 *@description 信息操作页面js
 *@author XieXing
 *@date 2020/8/23 15:33
 */
var table, form, layer, laydate, layedit, tree;//当前页面全局定义 TODO 继续优化
var module = {
    /**
     *@description 信息操作接口url集合 统一管理
     *@author XieXing
     *@date 2020/8/23 15:35
     */
    URL: {
        getDataListUrl: '/message/getList',//获取信息
        toEditPage: function (id) {
            if (id) {
                return "/message/edit?id=" + id;
            } else {
                return "/message/edit/";
            }
        },//跳转到信息编辑页面
        toDetailPage: function (id) { //跳转至信息详情页面
            return "/message/detail?id=" + id;
        },
        updatePublishStatusUrl: function (id, status) {//更新信息发布状态
            return '/message/' + id + '/' + status;
        },
        deleteUrl: function (id) {//删除信息
            return '/message/' + id;
        }
    },
    /**
     *@description 初始化
     *@author XieXing
     *@date 2020/8/23 15:36
     */
    init: function () {
        layui.use(['form', 'table', 'layer', 'laydate', 'layedit', 'tree'], function () {
            tree = layui.tree;
            table = layui.table;
            form = layui.form;
            layer = layui.layer;
            laydate = layui.laydate;
            layedit = layui.layedit;
            var pageData = page.pageData();
            table.render({
                elem: '#datagrid',
                id: 'datagrid',
                url: module.URL.getDataListUrl,/*换成你的接口*/
                method: "GET",
                cols: [[
                    {type: 'radio', fixed: 'left'},
                    {type: 'numbers', title: '序号', fixed: "left"},
                    {
                        field: 'msgTitle',
                        title: '信息名称',
                        align: 'center',
                        width: '30%'
                    },
                    {field: 'msgContent', title: '信息内容', width: '40%'},
                    {
                        field: '', title: '操作', align: 'center', width: '30%', fixed: 'right',
                        templet: function (d) {
                            var str = "<button type='button' class='layui-btn' lay-event='detail'>查看</button>";
                            str += "<button type='button' class='layui-btn layui-icon layui-icon-edit' lay-event='edit'>修改</button>";
                            if (d.isPub == 0) {
                                str += "<button type='button' class='layui-btn layui-icon layui-icon-release' lay-event='publish'>发布</button>"
                            } else {
                                str += "<button type='button' class='layui-btn layui-icon layui-icon-close-fill' lay-event='cancelPublish'>取消发布</button>"
                            }
                            str += "<button type='button' class='layui-btn layui-btn-danger layui-icon layui-icon-delete' lay-event='del'>删除</button>";
                            return str;
                        }
                    }
                ]],
                height: pageData.tableHeight,
                limit: pageData.limit,
                limits: pageData.limits,
                page: true,
                response: {
                    statusCode: 200
                },
                parseData: function (res) { //res 即为原始返回的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.stateInfo, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list //解析数据列表
                    };
                },
                done: function (res, curr, count) {
                    var perPageMax = $(".layui-laypage-limits select option:last-child").val();//每页最大条数
                    page.tableLoadAll(count, perPageMax);//加载全部
                }
            });

            //监听单元格事件
            table.on('tool(datagrid)', function (obj) {
                var rowData = obj.data;
                if (obj.event === 'detail') {//查看详情
                    window.open(module.URL.toDetailPage(rowData.id));
                }
                if (obj.event === "edit") {//修改信息
                    location.href = module.URL.toEditPage(rowData.id);
                }
                if (obj.event === 'del') {
                    layer.confirm("确认删除信息" + rowData.msgTitle + "吗？", function () {
                        var loading = layer.load(1);
                        $.post(module.URL.deleteUrl(rowData.id), {}, function (result) {
                            if (result && result.code === 200) {
                                layer.closeAll();
                                $("#btnSearch").trigger("click");
                            } else {
                                layer.close(loading);
                                layer.msg(result.err, {icon: 2, time: 2000});
                                return false;
                            }
                        });
                    });
                }
                if (obj.event === 'publish') {
                    layer.confirm("确认发布信息：" + rowData.msgTitle + "吗？", function () {
                        var loading = layer.load(1);
                        $.post(module.URL.updatePublishStatusUrl(rowData.id, 1), {}, function (result) {
                            if (result && result.code === 200) {
                                layer.closeAll();
                                $("#btnSearch").trigger("click");
                            } else {
                                layer.close(loading);
                                layer.msg(result.err, {icon: 2, time: 2000});
                                return false;
                            }
                        });
                    });
                }
                if (obj.event === 'cancelPublish') {
                    layer.confirm("确认取消发布信息：" + rowData.msgTitle + "吗？", function () {
                        var loading = layer.load(1);
                        $.post(module.URL.updatePublishStatusUrl(rowData.id, 0), {}, function (result) {
                            if (result && result.code === 200) {
                                layer.closeAll();
                                $("#btnSearch").trigger("click");
                            } else {
                                layer.close(loading);
                                layer.msg(result.err, {icon: 2, time: 2000});
                                return false;
                            }
                        });
                    });
                }
            });

            /**
             *@description 点击查询
             *@author XieXing
             *@date 2020/7/30 11:34
             */
            $("#btnSearch").on("click", function () {
                var qm = {};
                $("#queryForm").serializeArray().forEach(function (each) {
                    qm[each.name] = each.value;
                });
                var cur = parseInt($(".layui-laypage-skip input").val()) || 1;//当前页
                var limit = parseInt($(".layui-laypage-limits select").val()) || 10;//当前每页条数
                table.reload('datagrid', {
                    where: qm,
                    page: {curr: cur, limit: limit}
                });
                return false;
            });


            /**
             *@description 点击新增
             *@author XieXing
             *@date 2020/8/16 16:55
             */
            $("#btnAdd").click(function () {
                location.href = module.URL.toEditPage();
            });

        });
    }


};
module.init();//运行初始化函数
